<?php
/**
 * 客户转化
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ConversionController.class.php v1.0 2014-06-12 17:27:03 $
 */
namespace Admin\Controller;
class ConversionController extends CommonController{
    private $model;
    private $service;
    private $logic;
    private $confName = 'web_config';
    //允许写入文件的配置项
    private $AllowConfig = array(
                "SERVICE_SWITCH",
                "SERVICE_QQ",
                "SERVICE_WANGWANG",
                "SERVICE_FLOAT",
                "SERVICE_OPEN",
                "SERVICE_SKIN",
            );
    //需要重新分解成数组的配置
    private $_config_array = array(
        "SERVICE_WANGWANG", 
        "SERVICE_QQ"
    );
    private $_config_int = array(
        "SERVICE_SWITCH",
        "SERVICE_OPEN"
    );
    public function _initialize() {
        parent::_initialize();
    }
    
    /**
     * 在线客服
     */
    public function index() {
        if(IS_POST){
            $tmp_config = I("post.config");
            if(!isArrayAndNotEmpty($tmp_config)){
                return $this->errorReturn("无效操作！");
            }
            $config = array();
            foreach ($tmp_config as $key => $value) {
                if(in_array($key, $this->AllowConfig)){
                    if(in_array($key, $this->_config_array) && trim($value) != ""){
                        $config[$key] = explode(",", $value); 
                    }else if(in_array($key, $this->_config_int)){
                        $config[$key] = intval($value); 
                    }else{
                        switch ($value) {
                            case "true":
                                $value = true;
                                break;
                            case "false":
                                $value = false;
                                break;
                            case "null":
                                $value = null;
                                break;

                            default:
                                break;
                        }
                        $config[$key] = $value; 
                    }
                }
            }
            $web_conf = fast_cache($this->confName, '', YDCMS_DATA);

            $new_config = array_merge($web_conf, $config);
            if(false === (fast_cache($this->confName, $new_config, YDCMS_DATA))){
                return $this->errorReturn('客服信息设置失败！');
            }
            
            return $this->successReturn('客服信息设置成功！');
        }else{
            $conf = fast_cache($this->confName, '', YDCMS_DATA);
            //var_dump($conf);die;
            $this->assign($conf);
            $this->display();
        }
    }
    /**
     * 综合统计
     */
    public function stat() {
        if(IS_POST){
            $tmp_config = $_POST['stat'];//I("post.stat", "", NULL);
            if(!isArrayAndNotEmpty($tmp_config)){
                return $this->errorReturn("无效操作！");
            }
            $config = array();
            foreach ($tmp_config as $key => $value) {
                if($key == "STAT_CODE"){
                    $config[$key] = $value;
                    break;
                }
            }
            $web_conf = fast_cache($this->confName, '', YDCMS_DATA);

            $new_config = array_merge($web_conf, $config);
            if(false === (fast_cache($this->confName, $new_config, YDCMS_DATA))){
                return $this->errorReturn('综合统计设置失败！');
            }
            
            return $this->successReturn('综合统计设置成功！');
        }else{
            $conf = fast_cache($this->confName, '', YDCMS_DATA);
            $this->assign($conf);
            $this->display();
        }
    }
}
